﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Media;

namespace drawing_print.util
{
    class BitmapUtil
    {   

        /// <summary>
        /// 
        /// 调整图片对比度
        /// </summary>
        /// <param name="a"> 图片 </param>
        /// <param name="v"> 对比度值 </param>
        /// <returns>调整后的图片</returns>
        public static Bitmap image_duibidu(Bitmap a, double v)
        {
            BitmapData bmpData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int bytes = a.Width * a.Height * 3;
            IntPtr ptr = bmpData.Scan0;
            int stride = bmpData.Stride;
            unsafe
            {
                byte* p = (byte*)ptr;
                int temp;
                for (int j = 0; j < a.Height; j++)
                {
                    for (int i = 0; i < a.Width * 3; i++)
                    {
                        temp = (int)((p[0] - 127) * v + 127);
                        temp = (temp > 255) ? 255 : temp < 0 ? 0 : temp;
                        p[0] = (byte)temp;
                        p++;
                    }
                    p += stride - a.Width * 3;
                }
            }
            a.UnlockBits(bmpData);
            return a;
        }


        // 第二种方法：基于内存
        public static Bitmap duibiduOnMemory(Bitmap bitmap, int degree)
        {
            //if (bitmap == null)
            //{
            //    return null;
            //}
            double Deg = (100.0 + degree) / 100.0;

            int width = bitmap.Width;
            int height = bitmap.Height;

            int length = height * 3 * width;
            byte[] RGB = new byte[length];

            BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            System.IntPtr Scan0 = data.Scan0;
            System.Runtime.InteropServices.Marshal.Copy(Scan0, RGB, 0, length);

            double gray = 0;
            for (int i = 0; i < RGB.Length; i += 3)
            {
                for (int j = 0; j < 3; j++)
                {
                    gray = (((RGB[i + j] / 255.0 - 0.5) * Deg + 0.5)) * 255.0;
                    if (gray > 255)
                        gray = 255;

                    if (gray < 0)
                        gray = 0;
                    RGB[i + j] = (byte)gray;
                }
            }

            System.Runtime.InteropServices.Marshal.Copy(RGB, 0, Scan0, length);// 此处Copy是之前Copy的逆操作
            bitmap.UnlockBits(data);
            return bitmap;
        }




    //角度转换 
    // b 加true 减false
    public static int angleConvert(int x, Boolean b)
        {
            int duo = x % 90;
            if (b)
            {
                int r = 90 - duo + x;
                return r >= 360 ? 0 : r;
            }
            else
            {
                x = x == 0 ? 360 : x;
                duo = duo == 0 ? 90 : duo;
                int r = x - duo;
                return r < 0 ? 0 : r;
            }
        }



        /// <summary>
        /// 旋转图片角度 
        /// </summary>
        /// <param name="img"> 图片 </param>
        /// <param name="angle"> 角度 </param>
        /// <returns></returns>
        public static Bitmap RotateImage(Bitmap img, float angle)

        {

            angle = angle % 360;//弧度转换

            double radian = angle * Math.PI / 180.0;

            double cos = Math.Cos(radian);

            double sin = Math.Sin(radian);

            //原图的宽和高

            int w = img.Width;

            int h = img.Height;

            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));

            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));

            //目标位图

            Bitmap dsImage = new Bitmap(W, H, img.PixelFormat);

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage))

            {
                //背景色
                g.Clear(Color.White);

                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;

                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //计算偏移量

                Point Offset = new Point((W - w) / 2, (H - h) / 2);

                //构造图像显示区域：让图像的中心与窗口的中心点一致

                Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);

                Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);

                g.TranslateTransform(center.X, center.Y);

                g.RotateTransform(360 - angle);

                //恢复图像在水平和垂直方向的平移

                g.TranslateTransform(-center.X, -center.Y);

                g.DrawImage(img, rect);



                //重至绘图的所有变换

                g.ResetTransform();

                g.Save();

            }

            return dsImage;

        }

        /// <summary>
        /// 复制图片
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static Bitmap OnlyClone(Bitmap bitmap)
        {
            int width = bitmap.Width;
            int heigth = bitmap.Height;
            Rectangle rectangle = new Rectangle(0, 0, width, heigth);
            PixelFormat pixelFormat = bitmap.PixelFormat;
            Bitmap bitmap1 = bitmap.Clone(rectangle, pixelFormat);
            return bitmap1;
        }

    }
}
